<?php
class ome_ctl_admin_order_replace extends desktop_controller {

    var $name = "批量替换";

    function index() {
        $orderObj = $this->app->model("orders");
        $all_orders= $_POST['order_id'];
        $key_arr = array('isSelectedAll','app','ctl','act','flt','view','_finder','_DTYPE_BOOL');
        //全选待处理订单
        if ($_POST['isSelectedAll'] == '_ALL_') {
            $where = array(
                    'op_id' => kernel::single('desktop_user')->get_id(),
                    'assigned' => 'assigned',
                    'abnormal' => 'false',
                    'is_fail' => 'false',
                    'status' => 'active',
                    'custom_process_status' => array('unconfirmed', 'confirmed', 'splitting'));
            // 店铺条件
            if(isset($_POST['shop_id'])){
                $where['shop_id'] = $_POST['shop_id'];
            }else {
                if (!empty($this->shopIds)) {
                    $where['shop_id'] = $this->shopIds;
                }
            }
            //tab导航栏条件
             if ($_GET['view']) {
                    $view = $_GET['view'];
                    switch ($view) {
                        case 989899:
                            $where['pause']   = 'true';
                            break;
                        case 989898:
                            $where['pause'] = 'false';
                            $where['is_cod'] = 'true';
                            break;
                        default:
                            $where['pause'] = 'false';
                            if($view>=999000) break;
                            if (isset($where['order_confirm_filter'])) {
                                $where['order_confirm_filter'] .= sprintf(" AND (sdb_ome_orders.auto_status & %s = %s)", $view, $view);
                            } else {
                                $where['order_confirm_filter'] = sprintf("(sdb_ome_orders.auto_status & %s = %s)", $view, $view);
                            }
                            break;
                    }
                }
            // 超级管理员
            if(kernel::single('desktop_user')->is_super()){
                if(isset($where['op_id']))
                    unset($where['op_id']);

                if(isset($where['group_id']))
                    unset($where['group_id']);
            }
            //高级搜索其他的条件限制
             $otherwhere = $_POST;
             if($otherwhere){
                foreach($key_arr as $key_val){
                    if(!$otherwhere[$key_val]) continue;
                    unset($otherwhere[$key_val]);
                }
                //高级搜索时间条件
                if($otherwhere['_DTYPE_TIME']){
                    foreach($otherwhere['_DTYPE_TIME'] as $dity_time){
                        $filter_time = strtotime($otherwhere[$dity_time]." ".$otherwhere["_DTIME_"]["H"][$dity_time].":".$otherwhere["_DTIME_"]["M"][$dity_time]);
                        $where[$dity_time."|".$otherwhere["_".$dity_time."_search"]] = $filter_time;
                        unset($otherwhere[$dity_time]);
                        unset($otherwhere["_DTIME_"]);
                        unset($otherwhere["_".$dity_time."_search"]);
                    }
                    unset($otherwhere["_DTYPE_TIME"]);
                }
                $where = array_merge($otherwhere,$where);//合并条件
             }

            $order_ids = $orderObj->getList('order_id', $where);
            $all_orders= array();
            foreach($order_ids as $val){
                $all_orders[] = $val['order_id'];
            }

        }

        $order_idsstr = implode(',',$all_orders);
        $this->pagedata['ordercount'] = count($all_orders);
        $this->pagedata['order_ids']  = $order_idsstr;
        $this->display('admin/order/replace.html');
    }
//执行订单物料替换
    function doreplace() {
        $oOrder = $this->app->model('orders');
        $order_ids = $_POST['order_ids'];
        $order_idsarr = explode(',',$order_ids);
        $material  = $_POST['material'];
        $material_replace = $_POST['material_replace'];
        if($material == $material_replace){
            echo json_encode(array('result' => 'fail', 'msg' => "替换失败:替换物料和原物料一致"));
            return false;
        }
        $orders_list = array();
        $item_list   = array();
        foreach($order_idsarr as $order_id){
            $orders_list = $oOrder->getItemBranchStore($order_id);
            $item_list[$order_id] = $orders_list['goods'];
        }
        $result = kernel::single('ome_order_manualreplace')->orderReplace($order_idsarr,$item_list,$material,$material_replace,$msg);
        if(!$result){
            array_unshift($msg,"替换失败:");
            echo json_encode(array('result' => 'fail', 'msg' => $msg));
            return false;
        }
        if(!empty($msg) || isset($msg) || $msg != null){
            array_unshift($msg,"替换失败:");
        }else{
            $msg = array();
            array_unshift($msg,"替换成功!");
        }
        echo json_encode(array('result' => 'succ', 'msg' =>$msg));
        return true;

    }
}
